module WegoLint; end

class WegoLint::Rule

  attr_accessor :errors, :language, :description

  @@pattern = nil

  def initialize(description, language = :general)
    @errors = []
    @language = language
    @output = ""
    @description = description
  end

  def run(text)
    text.split("\n").each_with_index do |line, index|
      line = collapse_quotes(line) if self.class.collapse_quotes?
      line = collapse_hashes(line) if self.class.collapse_hashes?
      if line =~ self.class.pattern
        @errors << {message: description, line_number: index + 1 }
      end
    end
  end

  ##############################################################################
  private

  def self.block_not_padded
    # This won't work with chained blocks on one line
    no_space_beginning_block = /\{[\w\|'"].*\}/
    no_space_end_block = /\{.*[\w'"]\}/
    /(#{no_space_beginning_block}|#{no_space_end_block})/
  end

  def collapse_hashes(line)
    line.gsub(/\{.*?(\w+\:|\=\>).*?}/, "{ }")
  end

  def collapse_quotes(line)
    newline = line
    ['"', "'"].each do |char|
      newline.gsub!(/#{char}.+?#{char}/, "#{char}TEST#{char}")
    end
    newline
  end
end
